#!/bin/bash

#设置显示颜色
Cyan='\033[0;36m'
Default='\033[0;m'

#设置需要参数
author=""
projectName=""
sshRepo=""
confirmed="n"
organizationName="haier"
basePath=$(cd `dirname $0`; pwd)
currentPath=$PWD
libType=1

getAuthor() {
    read -p "请输入作者名: " author

    if test -z "$author"; then
        getAuthor
    fi
}

getProjectName() {
    read -p "请输入项目名: " projectName

    if test -z "$projectName"; then
        getProjectName
    fi
}

getLibType(){
    read -p "请选择库类型：1 .framework  2 .a ? " libType
    while [ "$libType" != "1" -a "$libType" != "2" ]
    do
        read -p "请选择库类型：1 .framework  2 .a ? " libType
    done
    echo "libType = $libType"
}

getInfomation() {
    getAuthor
    getProjectName
    getLibType
    
    echo -e "\n${Default}================================================"
    echo -e "  Author             :  ${Cyan}${author}${Default}"
    echo -e "  Project Name       :  ${Cyan}${projectName}${Default}"
    if [ $libType = "1" ]; then
        echo -e "  Library type       :  ${Cyan}.framework${Default}"
    else
        echo -e "  Library type       :  ${Cyan}.a${Default}"
    fi

    echo -e "================================================\n"
}

echo -e "\n"
while [ "$confirmed" != "y" -a "$confirmed" != "Y" ]
do
    if [ "$confirmed" == "n" -o "$confirmed" == "N" ]; then
        getInfomation
    fi
    read -p "确定? (y/n):" confirmed
done

targetPath=$currentPath/${projectName}

if [ -d $targetPath ]; then
    clearDir="n"
    read -p "file is exist, clear this dir?(y/n):" clearDir
    if [[ "$clearDir" != "y" ]] && [[ "$clearDir" != "Y" ]] && [[ "$clearDir" != "" ]]; then
        exit 0;
    fi
    rm -rf $targetPath

fi

# 创建目标工程
mkdir $targetPath

handleFrameWorkTemp(){

    cp -r "$basePath/Temp" "${targetPath}"
    # Temp文件内容重命名
    mv "${targetPath}/Temp" "${targetPath}/Project"
    mv "${targetPath}/Project/Temp.xcodeproj" "${targetPath}/Project/${projectName}.xcodeproj"

    mv "${targetPath}/Project/TempTests/TempTests.m" "${targetPath}/Project/TempTests/${projectName}Tests.m"
    mv "${targetPath}/Project/TempTests" "${targetPath}/Project/${projectName}Tests"

    mv "${targetPath}/Project/${projectName}.xcodeproj/xcshareddata/xcschemes/Temp.xcscheme"  "${targetPath}/Project/${projectName}.xcodeproj/xcshareddata/xcschemes/${projectName}.xcscheme"

}

handleStaticTemp(){
    cp -r "$basePath/TempLib" "${targetPath}"
    # Temp文件内容重命名
    mv "${targetPath}/TempLib" "${targetPath}/Project"
    mv "${targetPath}/Project/TempLib.xcodeproj" "${targetPath}/Project/${projectName}.xcodeproj"

    mv "${targetPath}/Project/TempLibTests/TempLibTests.m" "${targetPath}/Project/TempLibTests/${projectName}Tests.m"
    mv "${targetPath}/Project/TempLibTests" "${targetPath}/Project/${projectName}Tests"

    mv "${targetPath}/Project/${projectName}.xcodeproj/xcshareddata/xcschemes/TempLib.xcscheme"  "${targetPath}/Project/${projectName}.xcodeproj/xcshareddata/xcschemes/${projectName}.xcscheme"
}

handleWorkDirAndExample(){
    # 创建projectName文件拷贝ReplaceMe
    cp -r "$basePath/workdir" "${targetPath}"
    mv "${targetPath}/workdir" "${targetPath}/${projectName}"

    cp -r "$basePath/Example" "${targetPath}"

    # 将podspec文件拷贝到projectName文件中
    specFilePath="${targetPath}/${projectName}.podspec"
    cp -f $basePath/pod.podspec  "$specFilePath"
}

# 拷贝文件
if [ $libType = "1" ]; then
    handleFrameWorkTemp
else
    handleStaticTemp
fi

handleWorkDirAndExample

pbxprojPath="${targetPath}/Project/${projectName}.xcodeproj/project.pbxproj"
xcworkspacedataPath="${targetPath}/Project/${projectName}.xcodeproj/project.xcworkspace/contents.xcworkspacedata"
tempschemePath="${targetPath}/Project/${projectName}.xcodeproj/xcshareddata/xcschemes/${projectName}.xcscheme"
tempschemePlistPath="${targetPath}/Project/${projectName}.xcodeproj/xcuserdata/$USER.xcuserdatad/xcschemes/xcschememanagement.plist"
testPath="${targetPath}/Project/${projectName}Tests/${projectName}Tests.m"
podfilePath="${targetPath}/Project/Podfile"
examplePodfilePath="${targetPath}/Example/Podfile"

# 进行文本替换
echo "editing..."
sed -i "" "s%__ProjectName__%${projectName}%g" "$podfilePath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$specFilePath"

sed -i "" "s%__Author__%${author}%g" "$specFilePath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$xcworkspacedataPath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$tempschemePath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$tempschemePlistPath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$pbxprojPath"

sed -i "" "s%__OrganizationName__%${organizationName}%g" "$pbxprojPath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$testPath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$podfilePath"

sed -i "" "s%__ProjectName__%${projectName}%g" "$examplePodfilePath"

cd ${targetPath}/Example

pod install

say "finished"
echo "finished"
